<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2018 http://www.donglixia.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: 十万马 <962863675@qq.com>
// +----------------------------------------------------------------------
// | DateTime: 2018-02-09 16:17
// +----------------------------------------------------------------------

namespace app\admin_fussen\controller;

use app\admin_fussen\model\BasicBanner as BasicBannerModel;
use app\admin_fussen\model\BasicInfo as BasicInfoModel;
use think\Db;
use think\Request;
use think\Session;

class Banner extends Base
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->currentModel = new BasicBannerModel();//实例化当前模型
    }

    /**
     * 列表页
     * @return mixed
     */
    public function index()
    {
        /*获取Banner位置*/
        $BasicInfo = new BasicInfoModel();
        $locationList = $BasicInfo->getBasicList('top', 'banner');
        $this->assign('locationList' ,$locationList);
        return $this->fetch();
    }

    /**
     * 列表页，获取数据
     * @return mixed
     */
    public function getDataList()
    {
        $map = $this->getDataListMap();
        return $this->currentModel->where($map)->order('banner_id desc')->layTable(['location_text', 'state_text']);
    }

    private function getDataListMap()
    {
        $param = $this->request->param();
        if (!empty($param['location'])) {
            $map['location'] = $param['location'];//位置
        }

        $map['language'] = Session::get('config.language');
        return $map;
    }
    
    public function edit()
    {
        $param = $this->request->param();
        $banner_id = !empty($param['banner_id']) ? $param['banner_id'] : '';
        if (!empty($banner_id)) {
            //获取当前Banner信息
            $data = $this->currentModel->where('banner_id', $banner_id)->find();
            if (empty($data)) {
                $this->error('信息不存在');
            }
            $data = $data->toArray();
            $this->assign('data', $data);
        }

        //获取Banner位置
        $BasicInfo = new BasicInfoModel();
        $locationList = $BasicInfo->getBasicList('top', 'banner');
        $this->assign('locationList' ,$locationList);

        return $this->fetch();
    }

    /**
     * 保存
     */
    public function save()
    {
        $param = $this->request->param();
        if (empty($param)) {
            $this->error('没有需要保存的数据！');
        }

        try{
            //详情图片有变更，从临时文件夹移动图片
            if (!empty($param['img_url'])) {
                $old = !empty($param['banner_id']) ? Db::name('basic_banner')->where('banner_id', $param['banner_id'])->field('img_url')->find() : '';
                if (empty($old) || $param['img_url'] != $old['img_url']) {
                    $param['img_url'] = current(imgTempFileMove([$param['img_url']], 'admin_fussen/images/banner/'));//从临时文件夹移动图片
                    if (!empty($param['banner_id'])) {
                        $count = Db::name('basic_banner')->where('img_url', $old['img_url'])->count();//编辑且换图，则删除旧图片
                        //若其他地方没使用该旧图片，则删除
                        if ($count == 1){
                            delete_file($old['img_url']);
                        }
                    }
                }
            }

            $this->currentModel->save($param);
        } catch (\Exception $e) {
            $msg = !empty($this->currentModel->getError()) ? $this->currentModel->getError() : $e->getMessage();
            save_error_log($msg.' ['.$e->getFile().':'.$e->getLine().']');
            $this->error($msg);
        }

        $this->success('保存成功！', 'edit?banner_id='.$this->currentModel->banner_id);
    }

}

